﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace DspHex2Dat
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private int length = 0;

        private void button1_Click(object sender, EventArgs e)
        {
            this.openFileDialog1.Filter = "HEX文件(*.hex)|*.hex|所有文件(*.*)|*.*";
            if (this.openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string fileName = this.openFileDialog1.FileName;
                this.textBox1.Text = fileName;

                string str = LoadFile(fileName);
                this.richTextBox1.Clear();
                
                if (str.EndsWith(","))
                {
                    str = str.Substring(0, str.Length - 1);
                }
                string data = string.Format("#define BOOT_DATA_LEN {0}\n", length);
                this.richTextBox1.AppendText(data);
                data = string.Format("unsigned short BootData[BOOT_DATA_LEN] = \n", length);
                this.richTextBox1.AppendText(data);
                this.richTextBox1.AppendText("{\n");
                this.richTextBox1.AppendText(str);
                this.richTextBox1.AppendText("\n};");
            }
        }

        private string Byte2String(byte[] data) {
            string str = "";
            for (int i = 0; i < data.Length; i++)
            {
                /*
                int m = data[i] / 16;
                int n = data[i] % 16;

                if (m >= 10)
                {
                    m = 'A' + m - 10;
                }
                else {
                    m = m + '0';
                }

                if (n >= 10)
                {
                    n = 'A' + n - 10;
                }
                else
                {
                    n = n + '0';
                }
                */
                int m = data[i];
                if (m >= 10)
                {
                    m = 'A' + m - 10;
                }
                else
                {
                    m = m + '0';
                }
                str += (char)data[i];
            }
            return str;
        }

        private string LoadFile(string fileName) {
            byte[] byData = new byte[1];

            Decoder decoder = Encoding.Default.GetDecoder();
            FileStream file = new FileStream(fileName, FileMode.Open);
            file.Seek(0, SeekOrigin.Begin);

            file.Read(byData, 0, byData.Length);
            file.Read(byData, 0, byData.Length);
            int i = 0;
            int j = 1;

            string res = "    ";

            length = 0;
            while (file.Read(byData, 0, byData.Length) > 0)
            {
                if (byData[0] != 0x0a && byData[0] != 0x0d && byData[0] != 0x03)
                {

                    if (j % 6 == 1)
                    {
                        res += "0x";
                        res += Byte2String(byData);
                        length++;
                    }
                    if (j % 6 == 2)
                    {
                        res += Byte2String(byData);
                    }
                    if (j % 6 == 3)
                    {
                        //fputs(str,fp2);
                    }
                    if (j % 6 == 4)
                    {
                        res += Byte2String(byData);
                    }
                    if (j % 6 == 5)
                    {
                        res += Byte2String(byData);
                    }
                    if (j % 6 == 0)
                    {
                        res += ",";
                        
                        if (i > 10)
                        {
                            res += "\n";
                            res += "    ";
                            i = 0;
                        }
                        else
                        {
                            i++;
                        }

                    }
                    j++;
                }
            }

            file.Close();
            return res;
        }
    }
}
